<template>
  <div class="tab-bar-item" @click="itemClick">
    <!-- 有关于判定和类之类的东西,通过嵌套一个div,避免殃及其余插槽 -->
    <div v-if="!isActive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
  name: 'TabBarItem',
  props: {
    path: String,
    activeColor: {
      type: String,
      default: '#ff5777'
    }
  },
  computed: {
    isActive() {
      //当前活跃路由和标签路由一致们就能点亮激活样式
      return this.$route.path.indexOf(this.path) != -1
    },
    activeStyle() {
      //判断是否活跃,活跃则给颜色,不活跃则是空对象,没有颜色
      return this.isActive ? { color: this.activeColor } : {}
    }
  },
  methods: {
    itemClick() {
      this.$router.push(this.path)
    }
  }
}
</script>

<style type="text/css" scoped>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 14px;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 4px;
  margin-bottom: 2px;
  vertical-align: middle;
}
</style>
